package com.delio.client.pagos.ingresos;

import java.util.ArrayList;
import java.util.List;

import com.delio.client.idioma.DelioConstants;
import com.delio.client.pagos.pagosueldos.PagoSueldos;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.GridEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.Record;
import com.extjs.gxt.ui.client.util.IconHelper;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.NumberField;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.grid.CellEditor;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.EditorGrid;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.google.gwt.core.client.GWT;

public class PanelGridPagoIngreso extends Dialog {

	private DelioConstants k = GWT.create(DelioConstants.class);
	

	private static Button btnRegistrar;
	private static Button btnCancelar;
	
	private List<ColumnConfig> configs;
	
	private ColumnConfig column;
	private ColumnModel cm;
	
	public static EditorGrid<BeanModel> grid;
	
	public static TextField<String> txtNombre;
	public static NumberField txtMonto;

	public PanelGridPagoIngreso() {
		super();
		setHeading(k.sistemaDelio()); 
		
		setFrame(true);
		
		setButtons("");
		setSize(400,300);
		setModal(true);
		setLayout(new FitLayout());

		configs = new ArrayList<ColumnConfig>();

	
		column = new ColumnConfig();
		column.setId("nombre");
		column.setHeader(k.ingresoNoRegistrado());
		column.setWidth(100);

		txtNombre = new TextField<String>();
		//txtNombre.setAllowBlank(false);
		column.setEditor(new CellEditor(txtNombre));
		configs.add(column);
		
		
		column = new ColumnConfig();
		column.setId("horaentrada");
		column.setHeader(k.monto());
		column.setWidth(100);

	
		
		column = new ColumnConfig();
		column.setId("monto");
		column.setHeader(k.monto());
		column.setWidth(100);

		txtMonto = new NumberField();
		txtMonto.setPropertyEditorType(Double.class);
	//	txtMonto.setAllowBlank(false);
		column.setEditor(new CellEditor(txtMonto));
		configs.add(column);
			
		cm = new ColumnModel(configs);

		PagoIngresos.getPagoIngresos();
		grid = new EditorGrid<BeanModel>(PagoIngresos.store, cm);
		grid.setAutoExpandColumn("nombre");
		grid.setBorders(true);
		grid.addListener(Events.Attach, new Listener<GridEvent<BeanModel>>() {
			@Override
			public void handleEvent(GridEvent<BeanModel> be) {
				PagoIngresos.loader.load();

			}
		});

		txtNombre.addListener(Events.Detach, new Listener<BaseEvent>() {
			@Override
			public void handleEvent(BaseEvent be) {
				validar(txtNombre);
			}
		});


		
		txtMonto.addListener(Events.Detach, new Listener<BaseEvent>() {
			@Override
			public void handleEvent(BaseEvent be) {
				validar(txtMonto);
			}
		});
		
		btnCancelar = new Button(k.cancelar(),IconHelper.createPath("images/icons/cancel.png"),new SelectionListener<ButtonEvent>() {

					@Override
					public void componentSelected(ButtonEvent ce) {
						PagoIngresos.store.rejectChanges();
						

					}
				});
		
		btnRegistrar = new Button(k.registrar(),IconHelper.createPath("images/icons/save_all.png"),new SelectionListener<ButtonEvent>() {

					@Override
					public void componentSelected(ButtonEvent ce) {
						List<Record> re = PagoIngresos.store.getModifiedRecords();
						List<PagoIngreso> lista=new ArrayList<PagoIngreso>();
					
						for (Record r : re) {
							PagoIngreso pi = new PagoIngreso();
						    Double monto=Double.valueOf(String.valueOf(r.get("monto")));
							pi.setMonto(monto);
							pi.setIngreso_id(r.get("ingreso_id").hashCode());
							pi.setContrato_id(PagoIngresos.contrato_id);
							pi.setPeriodo(PagoIngresos.periodo);
							
							if (r.get("id") != null)
								pi.setId(r.get("id").hashCode());
							
							lista.add(pi);
							
						}

						PagoIngresos.save(lista);
						PagoIngresos.store.commitChanges();
						//btnRegistrar.setEnabled(false);
						PagoSueldos.loader.load();
					}
				});


		addButton(btnRegistrar);
		addButton(btnCancelar);

	//	btnRegistrar.setEnabled(false);
		setButtonAlign(HorizontalAlignment.CENTER);
		add(grid);

	}
	
	
	private void validar(TextField<String> txt){
//		if (txt.getValue() == null) {
//			if (PagoIngresos.store.getSortDir().equals(SortDir.ASC)|| PagoIngresos.store.getSortDir().equals(SortDir.NONE))
//				grid.getStore().remove(grid.getStore().getAt(0));
//			else if (PagoIngresos.store.getSortDir().equals(SortDir.DESC)|| PagoIngresos.store.getSortDir().equals(SortDir.NONE))
//				grid.getStore().remove(grid.getStore().getAt(grid.getStore().getCount() - 1));
//
//			btnRegistrar.setEnabled(false);
//
//		} else if (txt.getValue() != null)
//			btnRegistrar.setEnabled(true);
		
	}
	
	private void validar(NumberField txt){
//		if (txt.getValue() == null) {
//			if (PagoIngresos.store.getSortDir().equals(SortDir.ASC)|| PagoIngresos.store.getSortDir().equals(SortDir.NONE))
//				grid.getStore().remove(grid.getStore().getAt(0));
//			else if (PagoIngresos.store.getSortDir().equals(SortDir.DESC)|| PagoIngresos.store.getSortDir().equals(SortDir.NONE))
//				grid.getStore().remove(grid.getStore().getAt(grid.getStore().getCount() - 1));
//
//			btnRegistrar.setEnabled(false);
//
//		} else if (txt.getValue() != null)
//			btnRegistrar.setEnabled(true);
		
	}
}
